# -*- coding: utf-8 -*-
# created on 2018/05/18

from mathsolver.functions.base import *


# 等差中项性质
class DengChaXingZhiEqUpdate001(BaseFunction):
    """
    等比数列{a_{n}}的前n项和为S_{n},已知S_{1},2S_{2},3S_{3}成等差数列,则{a_{n}}的公比为().
    """
    def solver(self, *args):
        assert len(args) == 1
        assert len(args[0].sympify()) == 3
        left, middle, right = args[0].sympify()
        self.steps.append(["", "∵%s, %s, %s成等差数列" % (new_latex(left), new_latex(middle), new_latex(right))])
        self.steps.append(["", "∴%s" % (BaseEq([middle * 2, left + right]).printing())])
        answer = {}
        if 'Sequations' in self.known:
            seqs = self.search('Sequations')
        else:
            seqs = []
        seqs.append([middle * 2, left + right])
        answer['Sequations'] = seqs
        self.output.append(BaseShuLieValue(answer))
        self.label.add("等差中项满足的条件")
        return self


class DengChaXingZhiEqUpdate002(BaseFunction):
    """
    在等比数列{a_{n}}中,a_{2}-a_{1}=2,且2a_{2}为3a_{1}和a_{3}的等差中项,求a_{n}
    """
    def solver(self, *args):
        assert len(args) == 3
        left = args[0].sympify()
        middle = args[1].sympify()
        right = args[2].sympify()
        self.steps.append(["", "∵%s, %s, %s成等差数列" % (new_latex(left), new_latex(middle), new_latex(right))])
        eq = [middle * 2, left + right]
        self.steps.append(["", "∴%s" % (BaseEq(eq).printing())])
        self.label.add("等差中项满足的条件")
        answer = {}
        if 'Sequations' in self.known:
            seqs = self.search('Sequations')
        else:
            seqs = []
        seqs.append(eq)
        answer['Sequations'] = seqs
        self.output.append(BaseShuLieValue(answer))
        return self


class DengChaXingZhiEqUpdate(BaseFunction):
    CLS = [DengChaXingZhiEqUpdate001, DengChaXingZhiEqUpdate002]

    def solver(self, *args):
        r = None
        for cl in DengChaXingZhiEqUpdate.CLS:
            try:
                r = cl(self.known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
